<?php
require_once "config.php";
require_once dirname(__FILE__). '/util/connection.class.php';
require_once dirname(__FILE__). '/util/util.php';
set_time_limit(60*60);
ini_set('memory_limit', '512M');

$g_start = microtime(true);

$dbconn = Connection::getInstance();

$data = getData($dbconn);

echo "Get Raw Data: " . (microtime(true) - $g_start) . " seconds\n";
echo "Raw Data: " . count($data) . "\n";

$result = array();

foreach ($data as $d)
{
	$key = implode(":", array($d['site'], $d['pub'], $d['pla']));
	if (empty($result[$key]))
	{
		$result[$key] = array();
		$result[$key]['short_reg'] = $d['basic_reg'];
		$result[$key]['long_reg'] = $d['full_reg'];
	}
	else
	{
		$result[$key]['short_reg'] += $d['basic_reg'];
		$result[$key]['long_reg'] += $d['full_reg'];
	}
}
echo "Result Data: " . count($result) . "\n";
echo 'Totally: '. (microtime(true) - $g_start) . " seconds \n";
echo "memory: " . memory_get_peak_usage() . " bytes \n";

function getData($db)
{
	$sql = "select * from site_data where time_new>='2012-01-10' and time_new<'2012-01-11'";
	try
	{
		return $db->query($sql);
	}
	catch (Exception $e)
	{
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}
?>